from europe_pmc import EuropePMC
from europe_pmc import util
epmc = EuropePMC()


def search_europepmc(
        term = None,
        download_pdf=False,
        save_dir="./papers"
):
    """
    使用 epmc.EuropePMC 类检索 Europe PMC 论文，支持关键词、、PDF下载。
    """
    data =  epmc.fetch(term)

    results = [
        {
            "title": data.data.get("title"),
            "journalTitle": data.data.get("journalTitle"),
            "authors": data.data.get("authorString"),
            "published": data.data.get("firstPublicationDate"),
            "pdf_url": data.data.get("pdf_url"),
        }
    ]

    if download_pdf and data.data.get("hasPDF")=='Y':
        util.download(data.pdf_url, data.title + '.pdf' , save_dir)

    return results 